package com.cskaoyan.service;

import com.cskaoyan.bean.market.*;
import com.cskaoyan.mapper.RegionMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    @Override
    public RegionListVo list() {
        List<Region> regions = regionMapper.selectByExample(null);
        Byte typeid = regions.get(0).getType();
        RegionExample regionExample = new RegionExample();
        regionExample.createCriteria().andTypeEqualTo(typeid);
        List<Region> regions1 = regionMapper.selectByExample(regionExample);
        PageInfo<Region> regionPageInfo = new PageInfo<>(regions1);
        int total = (int) regionPageInfo.getTotal();
        int pages = regionPageInfo.getPages();
        //limit == total
        ArrayList<ListBean> listBeans = new ArrayList<>();
        for (Region region : regions1) {
            ListBean listBean = new ListBean();
            listBean.setId(region.getId());
            listBean.setType(region.getType());
            listBean.setName(region.getName());
            listBean.setCode(region.getCode());
            RegionExample regionExample1 = new RegionExample();
            regionExample1.createCriteria().andPidEqualTo(region.getId());
            List<Region> regions2 = regionMapper.selectByExample(regionExample1);
            ArrayList<ChildrenBeanX> childrenBeanXES = new ArrayList<>();
            for (Region region1 : regions2) {
                ChildrenBeanX childrenBeanX1 = new ChildrenBeanX();
                childrenBeanX1.setId(region1.getId());
                childrenBeanX1.setCode(region1.getCode());
                childrenBeanX1.setType(region1.getType());
                childrenBeanX1.setName(region1.getName());
                ArrayList<ChildrenBeanX.ChildrenBean> childrenBeans = new ArrayList<>();
                RegionExample regionExample2 = new RegionExample();
                regionExample2.createCriteria().andPidEqualTo(region1.getId());
                List<Region> regions3 = regionMapper.selectByExample(regionExample2);
                for (Region region2 : regions3) {
                    ChildrenBeanX.ChildrenBean childrenBean = new ChildrenBeanX.ChildrenBean();
                    childrenBean.setId(region2.getId());
                    childrenBean.setCode(region2.getCode());
                    childrenBean.setName(region2.getName());
                    childrenBean.setType(region2.getType());
                    childrenBeans.add(childrenBean);
                }
                childrenBeanX1.setChildren(childrenBeans);
                childrenBeanXES.add(childrenBeanX1);
            }
            listBean.setChildren(childrenBeanXES);
            listBeans.add(listBean);
        }
        RegionListVo regionListVo = new RegionListVo();
        regionListVo.setLimit(total);
        regionListVo.setPage(pages);
        regionListVo.setPages(pages);
        regionListVo.setTotal(total);
        regionListVo.setList(listBeans);
        return regionListVo;
    }
}
